<script setup>
import { ref } from 'vue'
import { artGetChannelsService, artDelChannelService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
// 导入组件
import ChannelEdit from '@/views/article/components/ChannelEdit.vue'
import { ElMessageBox } from 'element-plus'
const channelList = ref([])
// 设置加载特效 loading
const loading = ref(false)
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  console.log(res)
  channelList.value = res.data.data
  // 打印输出文章内容
  // 当请求的内容加载成功时，关闭loading效果
  loading.value = false
}
getChannelList()

// 添加弹框功能,使用子传父来进行接收
const dialog = ref()

// 编辑和删除功能
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确定要删除该分类的信息吗？', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessageBox({ type: 'success', message: '删除成功' })
  getChannelList()
}
// 添加操作
const onAddChannel = () => {
  console.log(dialog.value)
  dialog.value.open({})
}
// 编辑弹框功能
const onEditChannel = (row) => {
  dialog.value.open(row)
}
// 调用getChannelList获取列表
// 接收子组件传递过来的success,重新渲染列表
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <!-- 子类中的extra -->
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>

    <!--主体部分 -->
    <!-- 文章管理内容部分 -->
    <!-- 使用el-tale渲染表格 -->
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column label="序号" type="index" width="100"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"> </el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"> </el-table-column>
      <el-table-column label="操作" width="100">
        <!--使用插槽来进行编辑  -->
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="promary"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <!-- 当没有数据时 -->
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 从组件中导入弹窗组件使用ref来接收弹窗 -->
    <!-- 弹窗 -->
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>
<style lang="scss" scoped></style>
